package com.study.bootstudy.config;

import cn.hutool.core.util.ObjectUtil;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;

/**
 * @Author RenPu
 * @Date 2023/10/11 17:03
 * @Version 1.0
 * @Description: 设置自定义拦截规则,根据获取session中的值，进行拦截
 **/

public class MyInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Object username = request.getSession().getAttribute("username");
        if(ObjectUtil.isNotEmpty(username)){
            return true;
        }else {
            request.setAttribute("login_error","请先登录！！！！");
            request.getRequestDispatcher("/").forward(request,response);
            return false;
        }

    }
}
